package com.hl.card.credit.utill;

import org.bouncycastle.util.encoders.UrlBase64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import static com.hl.card.credit.utill.GenerateSignUtil.buildQueryStringMulti;
import static com.hl.card.credit.utill.GenerateSignUtil.getSHA256String;
import static com.hl.card.credit.utill.GenerateSignUtil.sortMapByKey;

public class StatusTool {

    public static  String getSign(Map map){
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        StringBuilder sb=new StringBuilder();
        while (iter.hasNext()) {
            String key = iter.next();
            System.out.println(key + ":"+ map.get(key));
            sb.append(key+"="+map.get(key)+"&");
        }
        String param= sb.subSequence(0,sb.length()-1).toString().toUpperCase();
        System.out.println(param);
        param="ORDER_ID=M180301729420180302104130012345678901";
        return  HMACSHA256.sha256_HMAC(param,HMACSHA256.secret_key);
    }

    public static String getSign2(Map map){
        String secret="Ued234fKsd3*45fLNs3-45dfk57s";
        String value;
        StringBuffer bf = new StringBuffer();
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            System.out.println(key + ":"+ map.get(key));
            bf.append("&");
            bf.append(key);
            bf.append("=");
            bf.append(map.get(key));
        }
        //将最终的字符串转换成全大写，再使用HmacSHA256进行签名。
        System.out.println("排序后拼接转大写："+bf.substring(1).toString().toUpperCase());
        value=bf.substring(1).toString().toUpperCase();
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            String hash =new String(UrlBase64.encode(sha256_HMAC.doFinal(value.getBytes("utf-8"))));
            return hash;
        } catch (Exception e) {
            System.out.println("Failed to generate the sign."+ e);
            return null;
        }
    }
    public static String getSign3(Map map){
        String secret="Ued234fKsd3*45fLNs3-45dfk57s";
        StringBuffer bf = new StringBuffer();
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        StringBuilder Aab=new StringBuilder();
        while (iter.hasNext()) {
            String key = iter.next();
            Aab.append(key);
            System.out.println(key + ":"+ map.get(key));
            bf.append("&");
            bf.append(key);
            bf.append("=");
            bf.append(map.get(key));
        }
        String  value=bf.substring(1).toString()+"&key="+secret;
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            String hash =new String(UrlBase64.encode(sha256_HMAC.doFinal(value.getBytes("utf-8"))));
            return hash;
        } catch (Exception e) {
            System.out.println("Failed to generate the sign."+ e);
            return null;
        }
    }

    public static String getSign4(Map map){
        Map<String, String[]> sortMapByKey = sortMapByKey(map);
        String buildQueryStringMulti = buildQueryStringMulti(sortMapByKey);
        String sign = null;
        try {
            sign = getSHA256String(buildQueryStringMulti);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return sign;

    }



}
